home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Development Tools & Languages / Macintosh Common Lisp Related / Lisp FAQ 14Sept94 / 5.Object-oriented Programming < prev    next >
Encoding:
Internet Message Format  |  1994-09-15  |  21.9 KB  |  [TEXT/ttxt]

  1. Subject: FAQ: Object-oriented Programming in Lisp 5/7 [Monthly posting]
  2. Newsgroups: comp.lang.lisp,comp.lang.clos,news.answers,comp.answers
  3. Summary: Questions about CLOS, PCL and object-oriented programming in Lisp
  4. Distribution: world
  5. Followup-To: poster
  6. Reply-To: ai+lisp-faq@cs.cmu.edu
  7. Approved: news-answers-request@MIT.Edu
  8.  
  9. Archive-name: lisp-faq/part5
  10. Last-Modified: Tue Sep 13 18:06:13 1994 by Mark Kantrowitz
  11. Version: 1.50
  12. Maintainer: Mark Kantrowitz and Barry Margolin <ai+lisp-faq@cs.cmu.edu>
  13. URL: http://www.cs.cmu.edu:8001/Web/Groups/AI/html/faqs/lang/lisp/top.html
  14. Size: 22443 bytes, 430 lines
  15.  
  16. ;;; ****************************************************************
  17. ;;; Answers to Frequently Asked Questions about Lisp ***************
  18. ;;; ****************************************************************
  19. ;;; Written by Mark Kantrowitz and Barry Margolin
  20. ;;; lisp_5.faq
  21.  
  22. This post contains Part 5 of the Lisp FAQ. It is cross-posted to the
  23. newsgroup comp.lang.clos because it contains material of interest to
  24. people concerned with CLOS, PCL and object-oriented programming in
  25. Lisp. The other parts of the Lisp FAQ are posted only to the
  26. newsgroups comp.lang.lisp and news.answers.
  27.  
  28. If you think of questions that are appropriate for this FAQ, or would
  29. like to improve an answer, please send email to us at ai+lisp-faq@cs.cmu.edu.
  30.  
  31. CLOS/PCL Questions (Part 5):
  32.  
  33.   [5-0]   What is CLOS (PCL) and where can I get it?
  34.           How do you pronounce CLOS? What is the Meta-Object Protocol (MOP)?
  35.   [5-1]   What documentation is available about object-oriented
  36.           programming in Lisp?  
  37.   [5-2]   How do I write a function that can access defstruct slots by
  38.           name?  I would like to write something like 
  39.           (STRUCTURE-SLOT <object> '<slot-name>).   
  40.   [5-3]   How can I list all the CLOS instances in a class?
  41.   [5-4]   How can I store data and CLOS instances (with possibly circular
  42.           references) on disk so that they may be retrieved at some later
  43.           time? (Persistent Object Storage)
  44.   [5-5]   Given the name of a class, how can I get the names of its slots?
  45.   [5-6]   Free CLOS software.
  46.  
  47. Search for \[#\] to get to question number # quickly.
  48.  
  49. In general, questions about object oriented programming in Lisp,
  50. especially questions about using CLOS or compiling PCL, should be
  51. directed to the newsgroup comp.lang.clos.
  52.  
  53. ----------------------------------------------------------------
  54. Subject: [5-0]  What is CLOS (PCL) and where can I get it?
  55.                 How do you pronounce CLOS?
  56.  
  57. CLOS (Common Lisp Object System) is the object-oriented programming
  58. standard for Common Lisp. It is the successor to Symbolics FLAVORS and
  59. Xerox LOOPS (Lisp Object Oriented Programming System). The acronym
  60. CLOS is pronouned either as "See-Loss" or "Closs", depending on taste.
  61. PCL (Portable Common Loops) is a portable CLOS implementation, and is
  62. available by anonymous ftp from 
  63.    parcftp.xerox.com:/pub/pcl/ [13.1.64.94]
  64. Also in the same directory are sources for CLX R5 and an inspecter.
  65.  
  66. Most Common Lisp implementations now include their own CLOS
  67. implementations. Common Lisp implementations with native CLOS include:
  68. MCL, {A}KCL, Allegro CL (including Allegro CL\PC), Ibuki, Lucid,
  69. Medley, Symbolics Genera, CLOE, and Harlequin Lispworks. CMU CL uses a
  70. customized version of PCL as their CLOS. However, not all native CLOS
  71. implementations have as detailed a meta-object protocol as PCL. For
  72. example, MCL 2.0 users sometimes use the july-1d version of PCL
  73. instead of the native CLOS for precisely this reason.
  74.  
  75. The book ``The Art of the Metaobject Protocol'' (see below) includes
  76. the CLOS Metaobject Protocol specification as chapters 5 and 6.  The
  77. sources for the MOP spec itself are available from 
  78.    parcftp.xerox.com:/pub/pcl/mop/ [13.1.64.94]
  79. as the file spec.tar.Z, but this is no substitute for buying the
  80. book, since the book contains a lot of useful explanatory material
  81. beyond the spec. The Closette files related to the book are also
  82. available from parcftp as the file closette.lisp.
  83.  
  84. The CLOS code repository is available by anonymous ftp to
  85.    nervous.cis.ohio-state.edu:/pub/lispusers/clos/ [128.146.61.200] 
  86. If you've got code you'd like to add to the repository, send mail to
  87. Arun Welch, commonloops-request@cis.ohio-state.edu. 
  88.  
  89. ----------------------------------------------------------------
  90. Subject: [5-1] What documentation is available about object-oriented
  91.                programming in Lisp? 
  92.  
  93. Books about object-oriented programming in Lisp include:
  94.  
  95.    1. dpANS CL describes the entire Common Lisp language, which includes the
  96.       CLOS standard.  Informally, CLtL2 can also be used to learn about CLOS, 
  97.       but please remember that CLtL2 is not an official X3J13 committee
  98.       document. (The presentation of CLtL2 differs from that of the draft
  99.       proposed standard, and some matters of fact have changed in the proposed
  100.       standard since the publication of CLtL2.)
  101.  
  102.    2. Sonya E. Keene
  103.       "Object-Oriented Programming in Common Lisp: 
  104.        A Programmer's Guide to CLOS"
  105.       Addison-Wesley (Reading, MA), 1989. 266 pages. ISBN 0-201-17589-4.
  106.            Tutorial introduction to CLOS with many examples and
  107.            a lot of good advice for designing large programs using CLOS.
  108.  
  109.    3. Jo A. Lawless and Molly M. Miller.
  110.       "Understanding CLOS: the Common Lisp Object System"
  111.       Digital Press, 1991. 192 pages.
  112.  
  113.    4. Gregor Kiczales, Jim des Rivieres, and Daniel G. Bobrow.
  114.       "The Art of the Metaobject Protocol"
  115.       MIT Press, 1991. 335 pages. ISBN 0-262-61074-4, $34.95.
  116.            The first part of the book presents a model CLOS implementation,
  117.            introduces the basic principles of metaobject protocols, and 
  118.            works through the key elements of the CLOS Metaobject Protocol.
  119.            The second half is the detailed specification of the CLOS
  120.            Metaobject Protocol. A simple working interpreter suitable
  121.            for experimentation is contained in an appendix.
  122.  
  123.    5. Robert R. Kessler and Amy R. Petajan.
  124.       "LISP, Objects, and Symbolic Programming"
  125.       Scott, Foresman and Company (Glenview, IL), 1988. 644 pages.
  126.            Includes a small Lisp compiler.
  127.  
  128.    6. A short introduction to CLOS written by Jeff Dalton of the
  129.       University of Edinburgh <J.Dalton@ed.ac.uk> is available by
  130.       anonymous ftp from  
  131.          aiai.ed.ac.uk:/lisp/random [192.41.104.6]
  132.       as the file clos-guide.
  133.  
  134.    7. Andreas Paepcke, editor.
  135.       "Object-Oriented Programming: The CLOS Perspective"
  136.       MIT Press, 1993. 400 pages, ISBN 0-262-16136-2 ($40).
  137.          This book is a collection of essays on the following topics:
  138.            -  Description of CLOS and its design philosophy.
  139.            -  The Metaobject Protocol and its use.
  140.            -  Comparison of CLOS with Smalltalk, Eiffel, Sather, and C++.
  141.            -  CLOS Uses and Methodology. Descriptions of two large CLOS
  142.               applications (Sun's LispView and a hybrid knowledge
  143.               representation tool) and an approach to documenting
  144.               object-oriented protocols (similar to that of AMOP).
  145.            -  Implementation details. Descriptions of TI CLOS for the
  146.               Explorer and PCL's method dispatch mechanism.
  147.  
  148. ----------------------------------------------------------------
  149. Subject: [5-2] How can I write a function that can access defstruct slots 
  150.                by name?  I would like to write something like 
  151.                (STRUCTURE-SLOT <object> '<slot-name>).
  152.  
  153. There is currently no portable, built-in way to access structure slots
  154. given only the name.  If your Common Lisp includes an implementation
  155. of CLOS that supports the meta-object protocol specified in the
  156. original X3J13 draft spec (document X3J13/88-003), then it probably will
  157. allow (SLOT-VALUE <object> '<slot-name>); however, not all
  158. implementations of CLOS currently provide this.  Lacking this, some
  159. implementations may provide implementation-dependent functions that
  160. allow access to structure slots by name; note that this may cause
  161. saved images to be larger, as some implementations normally open-code
  162. structure accessors and discard slot name information.
  163.  
  164. While it is not possible to write a fully general STRUCTURE-SLOT function,
  165. it is not very difficult to write version that handles specific structure
  166. types.  For instance, after defining:
  167.  
  168.    (defstruct spaceship name captain position velocity)
  169.  
  170. one may then define:
  171.  
  172.    (defun spaceship-slot (spaceship slot-name)
  173.      (ecase slot-name
  174.        (name (spaceship-name spaceship))
  175.        (captain (spaceship-captain spaceship))
  176.        (position (spaceship-position spaceship))
  177.        (velocity (spaceship-velocity spaceship))))
  178.  
  179. or using CLOS (generic functions):
  180.  
  181. (defgeneric spaceship-slot (spaceship slot-name)
  182.   (:method ((x spaceship) (slot (eql :name)))
  183.     (spaceship-name x))
  184.   (:method ((x spaceship) (slot (eql :captain)))
  185.     (spaceship-captain x))
  186.   (:method ((x spaceship) (slot (eql :position)))
  187.     (spaceship-position x))
  188.   (:method ((x spaceship) (slot (eql :velocity)))
  189.     (spaceship-velocity x)))
  190.  
  191. Another popular way to define this is:
  192.  
  193.    (defun spaceship-slot (spaceship slot-name)
  194.      (funcall (symbol-function
  195.                 (find-symbol (format nil "SPACESHIP-~A" slot-name)
  196.                              #.(package-name *package*)))
  197.               spaceship))
  198.  
  199. I personally recommend the first version.  It is likely to be much faster
  200. and more memory efficient than the second version.  It's also easy to get
  201. the second one wrong; many people forget to specify the package argument to
  202. FIND-SYMBOL, which can cause incorrect results when the package at run time
  203. is different from the one at compile time.  Even my version assumes that
  204. SPACESHIP-SLOT is being defined in a file that is in the same package as
  205. the one containing the structure definition; if this isn't the case,
  206. #.(PACKAGE-NAME *PACKAGE*) should be replaced by a string naming the
  207. correct package.
  208.  
  209. Another workaround is to define a MY-DEFSTRUCT macro that parses the
  210. defstruct arguments and expands into a call to DEFSTRUCT along with a
  211. definition of the runtime slot-accessor function.
  212.  
  213. Some non-portable techniques include the use of SYSTEM:STRUCTURE-REF
  214. in Lucid (LCL:STRUCTURE-REF in earlier versions of Lucid) and 
  215. EXCL:STRUCTURE-REF in Allegro. 
  216. ----------------------------------------------------------------
  217. Subject: [5-3] How can I list all the CLOS instances in a class?
  218.  
  219. There is no built-in way to enumerate the instances of a class.  If you are
  220. only interested in listing the instances of classes that you have defined,
  221. it is not very difficult to implement it as part of your class definition.
  222. Add a shared slot, e.g. ALL-INSTANCES, with an initial value of NIL, to the
  223. class definition.  Then write an after-method on INITIALIZE-INSTANCE for
  224. this class, which pushes the instance being initialized onto ALL-INSTANCES.
  225. Note that this must be done separately for each class that wants to maintain
  226. such a list; it can't be encapsulated in a mixin class, because all its
  227. dependent classes would share the same ALL-INSTANCES slot.  A compromise
  228. would be to use a mixin to define the INITIALIZE-INSTANCE after-method (and
  229. any other general-purpose methods that use the slot), but not the shared
  230. slot; it would be up to the descendant classes to define the slot at the
  231. level of the class hierarchy that is appropriate. You could also try
  232. defining the classes that need instance-recording as instances of a
  233. metaclass that holds the instance registry on the class object. The
  234. recording behavior could then be built-in to an after method on
  235. initialize-instance for the root class of the metaclass, or even
  236. allocate-instance. To allow for garbage collection of old instances,
  237. you will also need to define a generic function to remove the recorded
  238. instances from the list of instances.
  239. ----------------------------------------------------------------
  240. Subject: [5-4]  How can I store data and CLOS instances (with possibly 
  241.                 circular references) on disk so that they may be
  242.                 retrieved at some later time? (Persistent Object Storage)
  243.  
  244. There are two approaches to CLOS object persistence. The first uses
  245. regular CLOS facilities in concert with a standard file system. The
  246. second uses a more sophisticated storage facility, such as a database.
  247. The first solution provides just the persistence, and usually cannot
  248. retrieve only some of the objects from disc. The second goes beyond this
  249. by providing some or all of the facilities typically associated with
  250. databases: transaction management, concurrency control, queries,
  251. selective object materialization, etc. Below, the two solutions are
  252. addressed in turn.
  253.  
  254. There are two main techniques for the file system solution. The first
  255. involves using #. to compile the data into a file.  The second
  256. produces an ASCII representation which, when evaluated, will reproduce
  257. an equivalent set of data.
  258.  
  259. If the data you wish to save is stored in the variable *hash-table*,
  260. create a file containing just the lines
  261.         (in-package "YOUR-PACKAGE")
  262.         (setq *hash-table* '#.*hash-table*)
  263. and compile it. The #. macro performs read-time evaluation of the
  264. expression following the dot, and so this compiles the data into the
  265. file. You may then load the file to restore the data. However, the
  266. resulting binary file is not portable between Lisp implementations,
  267. and sometimes not even for the same Lisp on different platforms. Also,
  268. some Lisps will treat the data as constant, and place it on pages in
  269. memory that are marked read-only (after it is loaded). If one tries to
  270. later modify the data, these Lisps will signal an error. Lucid CL only
  271. puts such constants in a read-only area when they appear inside
  272. functions, so this should be safe. Allegro CL doesn't seem to complain
  273. about modification if the data is a cons. DEC's VAXLisp, however, has
  274. problems with #. circular structures in .fas files. MCL seems to work
  275. well with using #. to save data (and even functions) to a file.
  276.  
  277. The other technique is to produce an ASCII representation of the Lisp
  278. objects which may then be saved to a file. To reproduce the data, one
  279. can load (or compile and load) the file. This technique is portable
  280. between different Lisps and platforms. Unfortunately, the resulting
  281. data is not necessarily EQ to the original. Kerry Koitzsch's
  282. save-object.lisp package is included in the Lisp Utilities Repository,
  283.    ftp.cs.cmu.edu:/user/ai/lang/lisp/code/ext/save_obj/
  284. The Lisp Utilities Repository is described in detail in the answer to
  285. question [6-1]. 
  286.  
  287. See also the discussion of MAKE-LOAD-FORM and MAKE-LOAD-FORM-SAVING-SLOTS 
  288. in CLtL2.
  289.  
  290. Here are some solutions to object persistence that use databases instead
  291. of file systems.
  292.  
  293. Free Persistent Object Storage systems include: 
  294.  
  295.    WOOD (William's Object Oriented Database) is a simple persistent
  296.    object store for MCL 2.0, written by Bill St. Clair
  297.    <bill@cambridge.apple.com>.  Its goal is to provide a way to
  298.    save/restore Lisp objects to/from disk.  It is available by anonymous
  299.    ftp from the MCL repository on cambridge.apple.com:/pub/MCL2/CONTRIB/.
  300.    Send bug reports to bug-wood@cambridge.apple.com. To be added to the
  301.    mailing list, send mail to info-wood-request@cambridge.apple.com.
  302.  
  303.    PCLOS is a persistent object store for CLOS that was implemented using
  304.    CLOS's metaobject protocol. It allowed multiple different storage
  305.    facilities to be used. The benefit of this approach is it lets you use
  306.    different storage facilities depending on your needs, ranging from a
  307.    full-blown database with concurrency control and recovery to a very
  308.    fast store that trades speed for functionality. For example, an
  309.    in-memory store that saves its state to disk periodically can be much
  310.    faster than traditional databases, but risks loss of data due to
  311.    crashes. Unfortunately, PCLOS was built on a very old version of CLOS,
  312.    so it is unusable in its current form, and there are no plans to
  313.    update it. The introductory MOP chapter in the book "Object-Oriented
  314.    Programming: The CLOS Perspective" (see [5-1] above) explains how to
  315.    do the rewrite in principle and shows how the CLOS MOP was used for
  316.    object persistence in PCLOS. See also 
  317.       1. Andreas Paepcke, "PCLOS: A Flexible Implementation of CLOS 
  318.          Persistence", in Proceedings of ECOOP-88, S. Gjessing and K.
  319.          Nygaard, editors, Lecture Notes in Computer Science, Springer
  320.          Verlag, pages 374-389, 1988.  [Explains the PCLOS architecture.]
  321.       2. Andreas Paepcke, "PCLOS: A Critical Review", in OOPSLA-89, 1989. 
  322.          [Uses PCLOS as a roadmap through the issues of object persistence.]
  323.       3. Andreas Paepcke, "PCLOS Reference Manual", Hewlett-Packard
  324.          Laboratories, December, 1991.
  325.    For more information about PCLOS, write to Andreas Paepcke
  326.    <paepcke@parc.xerox.com>. 
  327.  
  328.    GBB (see [6-3]) is a blackboard architecture with persistence of a
  329.    sort. Every object in GBB is held in RAM, and there are dump and
  330.    restore functions. It handles distributed object delivery and
  331.    function-call triggering of receiving processes.
  332.  
  333. Commercial Persistent Object Storage systems include: 
  334.  
  335.    AllegroStore is a high-performance object-oriented database management
  336.    system from Franz. It offers Allegro CL users persistent object
  337.    storage with very fast retrieval and update of object data.  It
  338.    provides query processing and transaction-based operation. Built on a
  339.    multi-client, multi-server architecture, it permits concurrent access
  340.    over a network to objects by multiple independent processes.  It uses
  341.    the ObjectStore ODBMS from Object Design Inc, and thus has both Lisp
  342.    and C interfaces and is also very fast.  The CLOS interface is based
  343.    on MOP, with dynamic class/schema modification and redefinition.
  344.    AllegroStore can handle large-scale applications with minimal impact
  345.    on performance. It uses page-faulting and page-locking mechanisms
  346.    instead of object locking, providing high throughput and low overhead
  347.    for concurrent access by multiple users.  Standard database features
  348.    include deadlock detection, referential integrity, and inverse
  349.    functions.  Exception handling is integrated into the Lisp condition
  350.    system. AllegroStore runs on Sparc, SGI, HP, RS/6000, and
  351.    MS-Windows/NT systems.  For more information, send email to
  352.    info@franz.com, write to Franz Inc., 1995 University Avenue, Berkeley,
  353.    CA 94704, call 1-800-333-7260, 510-548-3600, fax 510-548-8253, or
  354.    telex 340179 WUPUBTLXSFO.
  355.  
  356.    ITASCA ODBMS V2.2 is a distributed active object database management
  357.    system. ITASCA allows clients to transparently access data that is
  358.    distributed among multiple servers.  ITASCA supports full dynamic
  359.    schema modification that can be performed during any phase of the
  360.    software lifecycle.  Applications written in dissimilar and
  361.    incompatible languages, such as C/C++ and CLOS, can share objects through
  362.    ITASCA. ITASCA stores methods inside the database, promoting
  363.    reusability and maintainability.  ITASCA is based on work at MCC's
  364.    Object-Oriented and Distributed Systems Lab on the ORION system. For
  365.    more information, write to Itasca Systems, Inc., 7850 Metro Parkway,
  366.    Minneapolis, MN 55425, sales@itasca.com, 612-851-3155, fax 612-851-3157.
  367.    [Clint Hyde has written a MOP CLOS interface to Itasca, which has some
  368.    features not present in their interface. For a free copy of his source
  369.    code, send him mail to chyde@bbn.com.]
  370.  
  371.    Statice is a commercial product from Symbolics, that provides a
  372.    powerful persistent ODBMS. It runs on Symbolics Lisp Machines and soon
  373.    on Unix boxes under Lucid. 
  374.  
  375. ----------------------------------------------------------------
  376. Subject: [5-5] Given the name of a class, how can I get the names
  377.                of its slots?     
  378.  
  379. (defun class-slot-names (class-name)
  380.   "Given a CLASS-NAME, returns a list of the slots in the class."
  381.   (mapcar #'clos:slot-definition-name
  382.           (clos:class-slots (find-class class-name))))
  383.  
  384. (defmethod class-slot-names ((instance standard-object))
  385.   "Given an INSTANCE, returns a list of the slots in the instance's class."
  386.   (mapcar #'clos:slot-definition-name
  387.           (clos:class-slots (class-of instance))))
  388.  
  389. You can use CLASS-DIRECT-SLOTS instead of CLASS-SLOTS if you don't
  390. want inherited slots. Note that these functions are from the
  391. meta-object protocol specified in the original X3J13 draft spec
  392. (document X3J13/88-003), and may not be supported by all Lisps.
  393.  
  394. ----------------------------------------------------------------
  395. Subject: [5-6] Free CLOS software.
  396.  
  397. Software Repositories:
  398.  
  399.    The CLOS code repository is available by anonymous ftp to
  400.    nervous.cis.ohio-state.edu:/pub/lispusers/clos/ [128.146.61.200]
  401.    If you've got code you'd like to add to the repository, send
  402.    mail to Arun Welch, commonloops-request@cis.ohio-state.edu.
  403.    The CLOS code repository includes dag.lisp.Z and 3DGeometry.lisp.
  404.  
  405. CORBA:
  406.  
  407.    The CORBA (Common Object Request Broker Architecture) specification's
  408.    Dynamic Invocation Interfaces (DII) are a collection of interfaces for
  409.    writing extensible, distributed object applications.  NEC Systems
  410.    Laboratory Inc. has contributed both C and CLOS implementations of
  411.    these specification to the Object Management Group (OMG). The CORBA
  412.    specification (91-8-1) is available free to OMG members, and for $50
  413.    to non-members. To order the document, send mail to documents@omg.org,
  414.    or write to OMG, 492 Old Connecticut Path, Framingham, MA 01701, phone
  415.    508-820-4300, fax 508-820-4303. The DII implementation can be
  416.    retrieved via anonymous ftp from omg.org:/pub/ [192.67.184.64] as the
  417.    file NEC_DII/93-1-2.tar.Z. There are three dependencies in the
  418.    contributed DII implementations.  First, the CLOS images require an
  419.    Allegro CL license due to its use of the Lisp foreign function
  420.    interface.  Second, the C executables require Sun SparcStations, but
  421.    they can be re-compiled and re-linked for other architectures.  Third,
  422.    the implementation of the `Invoke' DII interface is considered
  423.    proprietary.  For further information, write to Don Vines, NEC Systems
  424.    Laboratory, Inc., C&C Software Technology Center (CSTC), 1901 Gateway
  425.    Drive, Irving, Texas 75038, call 214-518-3486, fax 214-518-3552, or
  426.    send email to dvines@syl.dl.nec.com.
  427.  
  428. ----------------------------------------------------------------
  429. ;;; *EOF*
  430.  
  431.